{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /home/user/miniconda3/envs/yjq-3.6/lib/python3.6/site-packages/tensorflow/python/compat/v2_compat.py:96: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "non-resource variables are not supported in the long term\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "sys.path.append('../../common/')\n",
    "from evaluator import *\n",
    "import warnings\n",
    "import os\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from lstmvae import VAE_LSTM\n",
    "warnings.filterwarnings(\"ignore\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def load_dataset(dataset, machine):\n",
    "    folder = os.path.join(\"../../processed\", dataset)\n",
    "    if not os.path.exists(folder):\n",
    "        raise Exception(\"Processed Data not found.\")\n",
    "    loader = []\n",
    "    for file in [\"train\", \"test\", \"labels\"]:\n",
    "        file = machine + \"_\" + file\n",
    "        loader.append(np.load(os.path.join(folder, f\"{file}.npy\")))\n",
    "    ## 准备数据\n",
    "    train_data = loader[0]\n",
    "    test_data = loader[1]\n",
    "    labels = np.zeros((loader[2].shape[0], 1))\n",
    "    for i, row in enumerate(loader[2]):\n",
    "        if np.any(row == 1):\n",
    "            labels[i] = 1   \n",
    "    return (train_data, test_data, labels)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## SMD"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "total datas: 28\n"
     ]
    }
   ],
   "source": [
    "origin_file_list = os.listdir(\"../../data/SMD/interpretation_label\")\n",
    "file_name_list = []\n",
    "for origin_file in origin_file_list:\n",
    "    file_name_list.append(origin_file[:-4])\n",
    "file_name_list.sort()\n",
    "\n",
    "datas = {}\n",
    "for file_name in file_name_list:\n",
    "    train_data, test_data, labels = load_dataset(\"SMD\", file_name)\n",
    "    datas[file_name] = (train_data, test_data, labels)\n",
    "    # print(f\"train_data shape: {train_data.shape}\")\n",
    "    # print(f\"test_data shape: {test_data.shape}\")\n",
    "    # print(f\"labels shape: {labels.shape}\")\n",
    "print(f\"total datas: {len(datas)}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "machine-1-1\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 362.29157\n",
      "\n",
      "\n",
      "Training time 0.04 minutes\n",
      "machine-1-2\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 438.05181\n",
      "\n",
      "\n",
      "Training time 0.04 minutes\n",
      "machine-1-3\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 508.73321\n",
      "\n",
      "\n",
      "Training time 0.03 minutes\n",
      "machine-1-4\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 531.16986\n",
      "\n",
      "\n",
      "Training time 0.04 minutes\n",
      "machine-1-5\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 433.37675\n",
      "\n",
      "\n",
      "Training time 0.04 minutes\n",
      "machine-1-6\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 477.49122\n",
      "\n",
      "\n",
      "Training time 0.03 minutes\n",
      "machine-1-7\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 549.15997\n",
      "\n",
      "\n",
      "Training time 0.04 minutes\n",
      "machine-1-8\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 442.48100\n",
      "\n",
      "\n",
      "Training time 0.04 minutes\n",
      "machine-2-1\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 491.29042\n",
      "\n",
      "\n",
      "Training time 0.03 minutes\n",
      "machine-2-2\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 436.65258\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n",
      "machine-2-3\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 457.05573\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n",
      "machine-2-4\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 436.34690\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n",
      "machine-2-5\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 449.04478\n",
      "\n",
      "\n",
      "Training time 0.04 minutes\n",
      "machine-2-6\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 388.62155\n",
      "\n",
      "\n",
      "Training time 0.06 minutes\n",
      "machine-2-7\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 447.09612\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n",
      "machine-2-8\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 486.00700\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n",
      "machine-2-9\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 350.02578\n",
      "\n",
      "\n",
      "Training time 0.04 minutes\n",
      "machine-3-1\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 374.79578\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n",
      "machine-3-10\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 488.97758\n",
      "\n",
      "\n",
      "Training time 0.04 minutes\n",
      "machine-3-11\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 448.63457\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n",
      "machine-3-2\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 513.77592\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n",
      "machine-3-3\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 535.48595\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n",
      "machine-3-4\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 499.12662\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n",
      "machine-3-5\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 503.27144\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n",
      "machine-3-6\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 394.23570\n",
      "\n",
      "\n",
      "Training time 0.06 minutes\n",
      "machine-3-7\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 462.42867\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n",
      "machine-3-8\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 345.15350\n",
      "\n",
      "\n",
      "Training time 0.06 minutes\n",
      "machine-3-9\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 370.07098\n",
      "\n",
      "\n",
      "Training time 0.06 minutes\n"
     ]
    }
   ],
   "source": [
    "results = {}\n",
    "for machine in file_name_list:\n",
    "        print(machine)\n",
    "        train_data,test_data, labels = datas[machine]\n",
    "        train_df = pd.DataFrame(train_data)\n",
    "        test_df = pd.DataFrame(test_data)\n",
    "        algo = VAE_LSTM(sequence_length=5, intermediate_dim=20, z_dim=30, n_dim=train_df.shape[1], num_epochs=5,  batch_size=1024, verbose=False, stride=10)\n",
    "        algo.fit(train_df)\n",
    "        anomaly_score = np.mean(algo.predict(test_df)['error_tc'], axis=1)\n",
    "        result = bf_search(labels.squeeze(), anomaly_score.squeeze(), verbose = False, is_adjust=True)\n",
    "        results[machine] = result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "precision mean: 0.8571\n",
      "recall mean: 0.7398\n",
      "f1 mean: 0.7653\n",
      "f1* mean: 0.7941\n"
     ]
    }
   ],
   "source": [
    "f1_scores = [item['f1-score'] for item in list(results.values())]\n",
    "precisions = [item['precision'] for item in list(results.values())]\n",
    "recalls = [item['recall'] for item in list(results.values())]\n",
    "TNs = [item['TN'] for item in list(results.values())]\n",
    "TPs = [item['TP'] for item in list(results.values())]\n",
    "FNs = [item['FN'] for item in list(results.values())]\n",
    "FPs = [item['FP'] for item in list(results.values())]\n",
    "f1_mean = round(np.mean(f1_scores).item(),4)\n",
    "precision_mean = round(np.mean(precisions),4)\n",
    "recall_mean = round(np.mean(recalls),4)\n",
    "f1_star = round(2 * precision_mean * recall_mean / (precision_mean + recall_mean + 0.00001),4)\n",
    "print(f\"precision mean: {precision_mean}\")\n",
    "print(f\"recall mean: {recall_mean}\")\n",
    "print(f\"f1 mean: {f1_mean}\")\n",
    "print(f\"f1* mean: {f1_star}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## SMAP\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "origin_file_list = os.listdir(\"../../processed/SMAP\")\n",
    "file_name_set = set()\n",
    "for origin_file in origin_file_list:\n",
    "    file_name_set.add(origin_file.split(\"_\")[0])\n",
    "file_name_list = list(file_name_set)\n",
    "file_name_list.sort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "datas = {}\n",
    "for file_name in file_name_list:\n",
    "    train_data, test_data, labels = load_dataset(\"SMAP\", file_name)\n",
    "    # print(train_data.shape)\n",
    "    datas[file_name] = (train_data, test_data, labels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A-1\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 236.46981\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n",
      "A-2\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 339.98037\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n",
      "A-3\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 352.07047\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n",
      "A-4\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 343.96679\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n",
      "A-5\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 56.05279\n",
      "\n",
      "\n",
      "Training time 0.04 minutes\n",
      "A-6\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 80.87160\n",
      "\n",
      "\n",
      "Training time 0.03 minutes\n",
      "A-7\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 226.23381\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n",
      "A-8\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 62.13804\n",
      "\n",
      "\n",
      "Training time 0.04 minutes\n",
      "A-9\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 61.06531\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n",
      "B-1\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 286.48825\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n",
      "D-1\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 246.23460\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n",
      "D-11\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 308.16823\n",
      "\n",
      "\n",
      "Training time 0.04 minutes\n",
      "D-12\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 67.47600\n",
      "\n",
      "\n",
      "Training time 0.04 minutes\n",
      "D-13\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 170.83411\n",
      "\n",
      "\n",
      "Training time 0.06 minutes\n",
      "D-2\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 236.66772\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n",
      "D-3\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 240.02083\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n",
      "D-4\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 238.54767\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n",
      "D-5\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 321.65074\n",
      "\n",
      "\n",
      "Training time 0.04 minutes\n",
      "D-6\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 328.24130\n",
      "\n",
      "\n",
      "Training time 0.03 minutes\n",
      "D-7\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 322.21466\n",
      "\n",
      "\n",
      "Training time 0.04 minutes\n",
      "D-8\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 304.13156\n",
      "\n",
      "\n",
      "Training time 0.04 minutes\n",
      "D-9\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 304.05041\n",
      "\n",
      "\n",
      "Training time 0.03 minutes\n",
      "E-1\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 242.61780\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n",
      "E-10\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 272.94776\n",
      "\n",
      "\n",
      "Training time 0.04 minutes\n",
      "E-11\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 244.40753\n",
      "\n",
      "\n",
      "Training time 0.04 minutes\n",
      "E-12\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 246.60529\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n",
      "E-13\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 238.88008\n",
      "\n",
      "\n",
      "Training time 0.04 minutes\n",
      "E-2\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 242.12050\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n",
      "E-3\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 248.48130\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n",
      "E-4\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 245.74107\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n",
      "E-5\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 243.11639\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n",
      "E-6\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 241.32107\n",
      "\n",
      "\n",
      "Training time 0.04 minutes\n",
      "E-7\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 231.04205\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n",
      "E-8\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 245.79344\n",
      "\n",
      "\n",
      "Training time 0.04 minutes\n",
      "E-9\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 245.40022\n",
      "\n",
      "\n",
      "Training time 0.04 minutes\n",
      "F-1\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 235.69102\n",
      "\n",
      "\n",
      "Training time 0.06 minutes\n",
      "F-2\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 224.11655\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n",
      "F-3\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 256.54786\n",
      "\n",
      "\n",
      "Training time 0.04 minutes\n",
      "G-1\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 248.76725\n",
      "\n",
      "\n",
      "Training time 0.04 minutes\n",
      "G-2\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 305.47055\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n",
      "G-3\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 371.86606\n",
      "\n",
      "\n",
      "Training time 0.04 minutes\n",
      "G-4\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 331.27305\n",
      "\n",
      "\n",
      "Training time 0.03 minutes\n",
      "G-6\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 228.41791\n",
      "\n",
      "\n",
      "Training time 0.04 minutes\n",
      "G-7\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 337.98133\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n",
      "P-1\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 237.80949\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n",
      "P-2\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 240.03681\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n",
      "P-3\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 240.71853\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n",
      "P-4\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 321.72559\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n",
      "P-7\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 229.19330\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n",
      "R-1\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 225.16075\n",
      "\n",
      "\n",
      "Training time 0.04 minutes\n",
      "S-1\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 235.65394\n",
      "\n",
      "\n",
      "Training time 0.06 minutes\n",
      "T-1\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 245.94677\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n",
      "T-2\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 249.14223\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n",
      "T-3\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 234.86143\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n"
     ]
    }
   ],
   "source": [
    "results = {}\n",
    "for machine in file_name_list:\n",
    "        print(machine)\n",
    "        train_data,test_data, labels = datas[machine]\n",
    "        train_df = pd.DataFrame(train_data)\n",
    "        test_df = pd.DataFrame(test_data)\n",
    "        if train_data.shape[0] < 1024:\n",
    "                batch_size = 256\n",
    "        else:\n",
    "                batch_size = 1024\n",
    "        algo = VAE_LSTM(sequence_length=20, intermediate_dim=1, z_dim=20, n_dim=train_df.shape[1], num_epochs=5,  batch_size=batch_size, verbose=False)\n",
    "        algo.fit(train_df)\n",
    "        anomaly_score = np.mean(algo.predict(test_df)['error_tc'], axis=1)\n",
    "        result = bf_search(labels.squeeze(), anomaly_score.squeeze(), verbose = False, is_adjust=True)\n",
    "        results[machine] = result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "precision mean: 0.8577\n",
      "recall mean: 0.9909\n",
      "f1 mean: 0.8977\n",
      "f1* mean: 0.9195\n"
     ]
    }
   ],
   "source": [
    "f1_scores = [item['f1-score'] for item in list(results.values())]\n",
    "precisions = [item['precision'] for item in list(results.values())]\n",
    "recalls = [item['recall'] for item in list(results.values())]\n",
    "TNs = [item['TN'] for item in list(results.values())]\n",
    "TPs = [item['TP'] for item in list(results.values())]\n",
    "FNs = [item['FN'] for item in list(results.values())]\n",
    "FPs = [item['FP'] for item in list(results.values())]\n",
    "f1_mean = round(np.mean(f1_scores).item(),4)\n",
    "precision_mean = round(np.mean(precisions),4)\n",
    "recall_mean = round(np.mean(recalls),4)\n",
    "f1_star = round(2 * precision_mean * recall_mean / (precision_mean + recall_mean + 0.00001),4)\n",
    "print(f\"precision mean: {precision_mean}\")\n",
    "print(f\"recall mean: {recall_mean}\")\n",
    "print(f\"f1 mean: {f1_mean}\")\n",
    "print(f\"f1* mean: {f1_star}\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## MSL\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "origin_file_list = os.listdir(\"../../processed/MSL\")\n",
    "file_name_set = set()\n",
    "for origin_file in origin_file_list:\n",
    "    file_name_set.add(origin_file.split(\"_\")[0])\n",
    "file_name_list = list(file_name_set)\n",
    "file_name_list.sort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "datas = {}\n",
    "for file_name in file_name_list:\n",
    "    train_data, test_data, labels = load_dataset(\"MSL\", file_name)\n",
    "    datas[file_name] = (train_data, test_data, labels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C-1\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch   1    Loss 20.21849\n",
      "\n",
      "\n",
      "Training time 0.07 minutes\n",
      "C-2\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 17.93149\n",
      "\n",
      "\n",
      "Training time 0.06 minutes\n",
      "D-14\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 20.39018\n",
      "\n",
      "\n",
      "Training time 0.09 minutes\n",
      "D-15\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 22.27479\n",
      "\n",
      "\n",
      "Training time 0.08 minutes\n",
      "D-16\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 21.53168\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n",
      "F-4\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 20.48677\n",
      "\n",
      "\n",
      "Training time 0.08 minutes\n",
      "F-5\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 20.35714\n",
      "\n",
      "\n",
      "Training time 0.09 minutes\n",
      "F-7\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 19.93145\n",
      "\n",
      "\n",
      "Training time 0.07 minutes\n",
      "F-8\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 20.88866\n",
      "\n",
      "\n",
      "Training time 0.08 minutes\n",
      "M-1\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 21.10606\n",
      "\n",
      "\n",
      "Training time 0.08 minutes\n",
      "M-2\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 20.46019\n",
      "\n",
      "\n",
      "Training time 0.07 minutes\n",
      "M-3\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 26.39264\n",
      "\n",
      "\n",
      "Training time 0.07 minutes\n",
      "M-4\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 22.81190\n",
      "\n",
      "\n",
      "Training time 0.07 minutes\n",
      "M-5\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 22.91169\n",
      "\n",
      "\n",
      "Training time 0.07 minutes\n",
      "M-6\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 21.26515\n",
      "\n",
      "\n",
      "Training time 0.06 minutes\n",
      "M-7\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 21.60401\n",
      "\n",
      "\n",
      "Training time 0.07 minutes\n",
      "P-10\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 21.38757\n",
      "\n",
      "\n",
      "Training time 0.10 minutes\n",
      "P-11\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 22.73166\n",
      "\n",
      "\n",
      "Training time 0.11 minutes\n",
      "P-14\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 20.48300\n",
      "\n",
      "\n",
      "Training time 0.08 minutes\n",
      "P-15\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 23.59659\n",
      "\n",
      "\n",
      "Training time 0.09 minutes\n",
      "S-2\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 21.89655\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n",
      "T-12\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 18.20187\n",
      "\n",
      "\n",
      "Training time 0.06 minutes\n",
      "T-13\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 18.80419\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n",
      "T-4\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 23.63074\n",
      "\n",
      "\n",
      "Training time 0.08 minutes\n",
      "T-5\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 20.62192\n",
      "\n",
      "\n",
      "Training time 0.07 minutes\n",
      "T-8\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 17.45526\n",
      "\n",
      "\n",
      "Training time 0.06 minutes\n",
      "T-9\n",
      "WARNING:tensorflow:`tf.nn.rnn_cell.MultiRNNCell` is deprecated. This class is equivalent as `tf.keras.layers.StackedRNNCells`, and will be replaced by that in Tensorflow 2.0.\n",
      "\n",
      "\n",
      "Training...\n",
      "\n",
      "\n",
      "Epoch   1    Loss 16.66346\n",
      "\n",
      "\n",
      "Training time 0.05 minutes\n"
     ]
    }
   ],
   "source": [
    "results = {}\n",
    "for machine in file_name_list:\n",
    "        print(machine)\n",
    "        train_data,test_data, labels = datas[machine]\n",
    "        train_df = pd.DataFrame(train_data)\n",
    "        test_df = pd.DataFrame(test_data)\n",
    "        algo = VAE_LSTM(sequence_length=20, intermediate_dim=16, z_dim= 5, n_dim=train_df.shape[1], num_epochs=5,  batch_size=256, verbose=False)\n",
    "        algo.fit(train_df)\n",
    "        anomaly_score = np.mean(algo.predict(test_df)['error_tc'], axis=1)\n",
    "        result = bf_search(labels.squeeze(), anomaly_score.squeeze(), verbose = False, is_adjust=True)\n",
    "        results[machine] = result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "precision mean: 0.8506\n",
      "recall mean: 0.9756\n",
      "f1 mean: 0.8838\n",
      "f1* mean: 0.9088\n"
     ]
    }
   ],
   "source": [
    "f1_scores = [item['f1-score'] for item in list(results.values())]\n",
    "precisions = [item['precision'] for item in list(results.values())]\n",
    "recalls = [item['recall'] for item in list(results.values())]\n",
    "TNs = [item['TN'] for item in list(results.values())]\n",
    "TPs = [item['TP'] for item in list(results.values())]\n",
    "FNs = [item['FN'] for item in list(results.values())]\n",
    "FPs = [item['FP'] for item in list(results.values())]\n",
    "f1_mean = round(np.mean(f1_scores).item(),4)\n",
    "precision_mean = round(np.mean(precisions),4)\n",
    "recall_mean = round(np.mean(recalls),4)\n",
    "f1_star = round(2 * precision_mean * recall_mean / (precision_mean + recall_mean + 0.00001),4)\n",
    "print(f\"precision mean: {precision_mean}\")\n",
    "print(f\"recall mean: {recall_mean}\")\n",
    "print(f\"f1 mean: {f1_mean}\")\n",
    "print(f\"f1* mean: {f1_star}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "yjq-3.6",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
